/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */
define([ 'adminapp', 'bootstrap-daterangepicker', 'spinner'], function ($app) {
    $app.navactive('gift_list');

    var preview_tpl = Handlebars.compile($('#showpics-template').html());

    $('#intergral_spinner').spinner();
    $('#stock_spinner').spinner();

    $app.formvalidator({
        'gift.code'      : {
            required : true,
            maxlength: 30
        },
        'gift.name'      : {
            required : true,
            maxlength: 120
        },
        'gift.end_time'  : {
            required : true,
            maxlength: 20
        },
        'gift.begin_time': {
            required : true,
            maxlength: 20
        },
        'gift.stock'     : {
            required: true,
            number  : true
        },
        'gift.intergral'     : {
            required: true,
            number  : true
        },
        'gift.activity_rule'     : {
            maxlength: 350
        },
        'gift.explanation'     : {
            maxlength: 350
        },
        'gift.slogan'     : {
            maxlength: 9
        }

    }, 'giftForm');

    $app.single_uploader({
        'uploader'  : g.ctx + 'file/up?dpi=gift',
        'buttonText': '点击上传礼品封面',
        'width'     : 120,
        'hideButton': true,
        'height'    : 20
    }, 'cover_image_btn');

    $app.uploader({
        'uploader'       : g.ctx + 'file/up?dpi=bacvtity',
        'buttonText'     : '点击上传图片',
        'width'          : 140,
        'queueSizeLimit' : 10,
        'onUploadSuccess': function (file, data, response) {
            var jsonData = eval('(' + data + ')');
            if (jsonData['status'] == 'OK') {
                var attachement_path = jsonData['data'];
                var $moPrictures = $('#mo_prictures');
                var val = $moPrictures.val();
                if (val) {
                    $moPrictures.val((val + ",") + attachement_path);
                } else {
                    $moPrictures.val(attachement_path);
                }
                $('#picture-grid').append(preview_tpl({ctx: g.ctx, data: attachement_path.replace('.', '.thumbnail.'), preview: attachement_path}))
            }
        }
    }, 'prictureAttachmentUpload');
    return {};
});